const { createAddr, findAllAddr, updateAddr, removeAddr, setDefaultAddr } = require("../service/addr.service")

class AddrController {
    async create(ctx) {
        const user_id = ctx.state.user.id
        const { consinee, phone, address } = ctx.request.body
        const res = await createAddr({ user_id, consinee, phone, address })
        ctx.body = {
            code: 0,
            message: '添加地址成功',
            result: res
        }
    }
    async findAll(ctx) {
        const user_id = ctx.state.user.id
        const res = await findAllAddr(user_id)
        ctx.body = {
            code: 0,
            message: '查询地址成功',
            result: res
        }
    }
    async update(ctx) {
        const id = ctx.request.params.id
        const res = await updateAddr(id, ctx.request.body)
        ctx.body = {
            code: 0,
            message: '更新地址成功',
            result: res
        }
    }
    async remove(ctx) {
        const id = ctx.request.params.id
        const res = await removeAddr(id)
        ctx.body = {
            code: 0,
            message: '删除地址成功',
            result: res
        }
    }
    async setDefault(ctx) {
        const id = ctx.request.params.id
        const res = await setDefaultAddr(id)
        ctx.body = {
            code: 0,
            message: '设置默认地址成功',
            result: res
        }
    }
}

module.exports = new AddrController()